package logic

import (
	"context"

	"longbri/api/main/internal/svc"
	"longbri/api/main/internal/types"
	"longbri/rpc/pb/dashboard"

	"github.com/zeromicro/go-zero/core/logx"
)

type ExceptLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewExceptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ExceptLogic {
	return &ExceptLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ExceptLogic) Except(req *types.ReqExceptCode) (resp *types.RspExceptCode, err error) {
	// todo: add your logic here and delete this line
	/*
		StartDate:      req.StartDate,
		EndDate:        req.EndDate,
		CurPageNum:     int32(req.PageNum),
		PerPageCount:   int32(req.PerPageCount),
		UpDivChange:    int32(req.UpDivChange),
		MarketValStart: int32(req.MarketValStart),
		MarKetValEnd:   int32(req.MarKetValEnd),
		UpRation:       int32(req.UpRatio),
		ChangeRatio:    int32(req.ChangeRatio),
	*/

	rsp, err := l.svcCtx.Dashboard.ExceptCode(l.ctx, &dashboard.ReqExceptCode{
		Id:    uint64(req.Id),
		Ahead: req.Ahead,
		Count: uint32(req.Count),
	})

	if err != nil {
		logx.Error(err)
		return &types.RspExceptCode{}, err
	}

	CodeList := []types.ExceptCode{}
	for i := 0; i < len(rsp.CodeList); i++ {
		tmp := rsp.CodeList[i]
		CodeList = append(CodeList, types.ExceptCode{
			Id:        int(tmp.Id),
			Code:      tmp.Code,
			ExceptTs:  int(tmp.ExceptTs),
			ExceptVol: int(tmp.ExceptVol),
			NewsTitle: tmp.NewsTitle,
			NewsTs:    int(tmp.NewsTs),
			NewsFrom:  tmp.NewsFrom,
			NewsUrl:   tmp.NewsUrl,
		})
	}

	return &types.RspExceptCode{
		CodeList: CodeList,
	}, nil
}
